package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.CommentDtoIn;
import com.zyw.zhuangdian.pojo.out.CommentDtoOut;
import com.zyw.zhuangdian.service.CommentService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
@Slf4j
public class CommentController {
    private final CommentService commentService;

    /**
     * 新增评论
     * @param json
     * @return
     */
    @RequestMapping(value = "addComment",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addComment(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"aid","评论目标不能为空");
        Assert.hasKeyAndValue(json,"uid","发布人不能为空");
        Assert.hasKeyAndValue(json,"type","评论类型不能为空");
        Assert.hasKeyAndValue(json,"content","评论内容不能为空");

        CommentDtoIn in = BeanConvertUtil.covertBean(json, CommentDtoIn.class);
        Resp<Void> resp = commentService.addComment(in);
        return RespUtil.OK(resp);
    }

    /**
     * 修改评论
     * @param json
     * @return
     */
    @RequestMapping(value = "updateComment",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> updateComment(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"cid","主键ID不能为空");

        CommentDtoIn in = BeanConvertUtil.covertBean(json, CommentDtoIn.class);
        Resp<Void> resp = commentService.updateComment(in);
        return RespUtil.OK(resp);
    }

    /**
     * 删除评论
     * @param json aid
     * @return
     */
    @RequestMapping(value = "deleteComment",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteComment(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"cid","主键ID不能为空");

        CommentDtoIn in = BeanConvertUtil.covertBean(json, CommentDtoIn.class);
        Resp<Void> resp = commentService.deleteComment(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出评论
     * @param json
     * @return
     */
    @RequestMapping(value = "listComment",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listComment(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"type","类型不能为空");

        CommentDtoIn in = BeanConvertUtil.covertBean(json, CommentDtoIn.class);
        Resp<List<CommentDtoOut>> resp = commentService.listComments(in);
        return RespUtil.OK(resp);
    }
}
